package com.learning.imoney;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import android.Manifest;


import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.learning.imoney.db.Bill;
import com.learning.imoney.db.DataBaseBill;

import java.util.List;

public class BillMainActivity extends AppCompatActivity implements View.OnClickListener {

    private BottomNavigationView bottomNavigationView;
    private FloatingActionButton addButton;
    private MaterialToolbar toolbar;
    private List<Bill> billList;

    private String date;
    private DataBaseBill mhelper;//定义数据库帮助类对象
    private SQLiteDatabase db;//定义一个可以操作的数据库对象

    private Fragment first, second, activeFragment;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_bill_main);

        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        //添加账单按钮
        addButton = findViewById(R.id.btn_add);
        addButton.setOnClickListener(this);

        //创建或获取数据库
        mhelper = new DataBaseBill(BillMainActivity.this);//实例化数据库帮助类
        db = mhelper.getWritableDatabase();//创建数据库，获取数据库的读写权限
//        ContentValues values = new ContentValues();//用 value 表示一行
//        values.put("money","1000");//
//        values.put("time","2024-10-16");//
//        values.put("type",1);//
//        values.put("note","中奖了！");//
//        //将这一行数据存放到数据库的数据表中。参数：（表名，某些为空的列自动赋值 null，ContentValue 对象）
//        db.insert("bill_info",null,values);
        // 初始化Fragment实例
        first = new OverviewFragment();
        second = new CalendarFragment(); // 另一个Fragment
        fragmentManager = getSupportFragmentManager();

        // 默认展示第一个Fragment
        fragmentManager.beginTransaction()
                .add(R.id.fragment_container, first, "overview")
                .add(R.id.fragment_container, second, "calendar")
                .hide(second)
                .commit();

        activeFragment = first;

        // 初始化底部导航栏，显示当月账单
        initView(savedInstanceState);


    }


//    @Override
//    protected void onResume() {
//        super.onResume();
//        Fragment selectedFragment = new OverviewFragment();
//        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
//
//    }

    private void initView(Bundle savedInstanceState) {
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;
                if (item.getItemId() == R.id.nav_overview) {
                    switchFragment(first);
                    //selectedFragment = new OverviewFragment();
                    toolbar.setTitle("收支");
                } else if (item.getItemId() == R.id.nav_calendar) {
                    switchFragment(second);
//                    selectedFragment = new calendarFragment();
                    toolbar.setTitle("日历");

                }
                //getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
                return true;
            }
        });
        // 默认选择第一个页面
        if (savedInstanceState == null) {
            bottomNavigationView.setSelectedItemId(R.id.nav_overview);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_add) {
            Intent intent = new Intent(this, BillAdd.class);
            startActivity(intent);
            Toast.makeText(this, "点击了add按钮", Toast.LENGTH_SHORT).show();
        }
    }

    private void switchFragment(Fragment fragment) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(activeFragment); // 隐藏当前显示的Fragment
        transaction.show(fragment); // 显示选中的Fragment
        transaction.commit();
        activeFragment = fragment; // 更新当前显示的Fragment
    }


}